<template>
  <div class="dashboard-container">
    <h1 style="text-align: center">主页</h1>
    <div style="font-size: 24px">
      <span class="income-text" style="margin-left: 20%">当月收入为：{{ this.revenue }} 元</span>
      <!-- 日期选择器，用于选择查询的月份 -->
      <el-date-picker
        v-model="months"
        type="month"
        value-format="yyyy-MM"
        placeholder="选择月"
        class="date-picker"/>
      <el-button class="action-button" @click="updateMonth">查询</el-button>
      <el-button @click="selectMonth" class="action-button" style="margin-bottom:30px;background-color: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);">重置</el-button>
    </div>
    <!-- 饼状图 -->
    <div class="chart-row">
      <div class="chart-container" >
        <!-- 饼状图组件，传入数据和配置 -->
        <PieChart :data="this.pieData" :options="pieOptions" />
      </div>

      <!-- 柱状图 -->
      <div class="chart-container" >
        <!-- 柱状图组件，传入数据和配置 -->
        <BarChart :data="this.barData" :options="barOptions" />
      </div>
    </div>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import PieChart from '@/components/PieChart.vue'
import BarChart from '@/components/BarChart.vue'
import { selectAllUser } from '@/api/user'
import { selectLiveNumber, selectRevenue, selectRevenueByMonth, selectRoomNumber } from '@/api/dashboard'

export default {
  name: '主页',
  components: { PieChart, BarChart },
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data(){
    return{
      months:'',
      roomNumber:'',
      liveNumber:'',
      revenue:'',
      revenueByMonth:'',
      pieData:[],
      barData:[]
    }
  },
  mounted() {
    this.selectMonth()
    this.createPieChart()
  },
  methods:{
    // 查询房间数量
    async selectRoomNumber() {
      await selectRoomNumber({}).then(
        response => {
          this.roomNumber = response.data
          console.log('房间数量'+this.roomNumber)
        }
      )
    },
    // 查询入住数量
    async selectLiveNumber() {
      await selectLiveNumber({}).then(
        response => {
          this.liveNumber = response.data
          console.log('入住数量'+this.liveNumber)
        }
      )
    },
    // 初始化月份选择
    selectMonth(){
      let date = new Date()
      // 获取当前年份
      let year = date.getFullYear()
      // 获取当前月份，注意月份是从0开始的，所以需要加1
      let month = date.getMonth() + 1
      if (month < 10) {
        this.months = year + "-0" + month
      }else {
        this.months = year + "-" + month
      }
      this.selectRevenue()
      this.selectRevenueByMonth()
    },
    // 更新月份后的查询操作
    updateMonth(){
      this.selectRevenue()
      this.selectRevenueByMonth()
    },
    // 查询当月收入
    selectRevenue() {
      selectRevenue({month:this.months}).then(
        response => {
          this.revenue = response.data
          console.log('当月收入'+this.revenue)
        }
      )
    },
    // 按月份查询收入
    selectRevenueByMonth() {
      selectRevenueByMonth({month:this.months}).then(
        response => {
          this.revenueByMonth = response.data
          this.barData = [
            {label:response.data[5].month,value:response.data[5].money,color:'#f4b7b0'},
            {label:response.data[4].month,value:response.data[4].money,color:'#f8f8b8'},
            {label:response.data[3].month,value:response.data[3].money,color:'#adfacf'},
            {label:response.data[2].month,value:response.data[2].money,color:'#b0f4ef'},
            {label:response.data[1].month,value:response.data[1].money,color:'#baddfb'},
            {label:response.data[0].month,value:response.data[0].money,color:'#d9b3f3'}
          ]
          console.log('房间数量')
          console.log(this.revenueByMonth)
        }
      )
    },
    // 生成饼图数据
    async createPieChart(){
      await this.selectRoomNumber()
      await this.selectLiveNumber()
      this.pieData = [
        {label:'入住房间',value:this.liveNumber,color:'rgb(241,226,167)'},
        {label:'剩余房间',value:this.roomNumber-this.liveNumber,color:'rgb(181,202,246)'},
      ]
    },

  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.chart-row {
  display: flex;
  justify-content: flex-start; // 改为靠左对齐
  flex-wrap: wrap;
  padding: 1%;
  background-color: rgba(255, 255, 255, 0.85);
  border: 1px solid #f3e8e8;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.chart-container {
  padding: 20px;
  text-align: center;
  width: 45%;
  margin: 20px 0;
  transition: all 0.3s ease-in-out;
}
.income-text,
.date-picker,
.action-button {
  margin-right: 10px;
  margin-bottom: 10px;
  margin-left: 40px;
}
.action-button{
  width:80px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  margin-bottom:30px;
  background-color: #cfefc6;
  border-radius: 8px;
}
</style>
